package com.iyoudoock.citymine.widget;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.adapter.CityAdapter;
import com.iyoudoock.citymine.adapter.DistrictAdapter;
import com.iyoudoock.citymine.adapter.ProvinceAdapter;
import com.iyoudoock.citymine.widget.wheel.OnWheelChangedListener;
import com.iyoudoock.citymine.widget.wheel.WheelView;
import com.kvin.toolkit.utils.PopupUtils;
import com.kvin.toolkit.utils.region.CityBean;
import com.kvin.toolkit.utils.region.DistrictBean;
import com.kvin.toolkit.utils.region.ProvinceBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kvin on 2017/6/2.
 */
public class RegionPop {
    private PopupUtils mRegionPop;
    private Context context;
    private TextView cancelTv;
    private TextView confirmTv;
    private WheelView provinceWv;
    private WheelView cityWv;
    private WheelView districtWv;

    private ArrayList<ProvinceBean> provinceList;
    private ArrayList<CityBean> cityList;
    private ArrayList<DistrictBean> districtList;
    private ProvinceAdapter provinceAdapter;


    private RegionPop(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.pop_region_choose, null);
        this.mRegionPop = PopupUtils.newInstance(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.context = context;
        this.cancelTv = (TextView) v.findViewById(R.id.cancel_tv);
        this.confirmTv = (TextView) v.findViewById(R.id.confirm_tv);
        this.provinceWv = (WheelView) v.findViewById(R.id.province_wv);
        this.cityWv = (WheelView) v.findViewById(R.id.city_wv);
        this.districtWv = (WheelView) v.findViewById(R.id.district_wv);


        provinceList = new ArrayList<>();
        cityList = new ArrayList<>();
        districtList = new ArrayList<>();

        provinceAdapter = new ProvinceAdapter(context, provinceList);

        //bind adapter
        this.provinceWv.setViewAdapter(provinceAdapter);

        this.provinceWv.addChangingListener(mOnWheelChangedListener);
        this.cityWv.addChangingListener(mOnWheelChangedListener);
    }

    public static RegionPop getInstance(Context context) {
        return new RegionPop(context);
    }


    /**
     * wheel changed listener
     */
    private OnWheelChangedListener mOnWheelChangedListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            Log.d("-----", "old=" + oldValue + " new=" + newValue);
            if (oldValue == newValue) return;
            if (wheel == provinceWv) {
                if (cityList.size() > 0) {
                    cityList.clear();
                }
                if (districtList.size() > 0) {
                    districtList.clear();
                }
                cityList.addAll(provinceList.get(newValue).getCityList());
                districtList.addAll(provinceList.get(newValue).getCityList().get(0).getDistrictList());
                cityWv.setViewAdapter(new CityAdapter(context, cityList));
                districtWv.setViewAdapter(new DistrictAdapter(context, districtList));
                cityWv.setCurrentItem(0);
                districtWv.setCurrentItem(0);
            } else if (wheel == cityWv) {
                if (districtList.size() > 0) {
                    districtList.clear();
                }
                districtList.addAll(cityList.get(newValue).getDistrictList());
                districtWv.setViewAdapter(new DistrictAdapter(context, districtList));
            }
        }
    };


    /**
     * set region
     */
    public void setRegion(List<ProvinceBean> data) {
        if (data == null) {
            throw new NullPointerException("data is null");
        }
        provinceList.addAll(data);
        cityList.addAll(data.get(0).getCityList());
        districtList.addAll(data.get(0).getCityList().get(0).getDistrictList());
        provinceAdapter.notifyDataChangedEvent();

        provinceWv.setCurrentItem(0);
        cityWv.setCurrentItem(0);
        districtWv.setCurrentItem(0);
    }


    /**
     * cancel or submit click event
     */
    public void setOnCancelClickListener(View.OnClickListener listener) {
        if (this.cancelTv != null)
            this.cancelTv.setOnClickListener(listener);
    }

    public void setOnSubmitClickListener(View.OnClickListener listener) {
        if (this.confirmTv != null)
            this.confirmTv.setOnClickListener(listener);
    }

    /**
     * show  or hide
     */
    public void show(View anchor, int gravity) {
        if (this.mRegionPop != null)
            this.mRegionPop.show(anchor, gravity);
    }

    /**
     * hide
     */
    public void dismiss() {
        if (this.mRegionPop != null)
            this.mRegionPop.dismiss();
    }

    /**
     * get address
     */
    public String getAddress(String symbol) {
        String p = getProvince();
        String c = getCity();
        String d = getDistrict();

        if (TextUtils.isEmpty(symbol)) return p + c + d;
        return p + symbol + c + symbol + d;
    }

    public String getAddress() {
        return getAddress("");
    }

    /**
     * return province,city,district
     */
    public String getProvince() {
        String p = provinceList.get(provinceWv.getCurrentItem()).getName();
        return p;
    }

    public String getCity() {
        String p = cityList.get(cityWv.getCurrentItem()).getName();
        return p;
    }

    public String getDistrict() {
        String p = districtList.get(districtWv.getCurrentItem()).getName();
        return p;
    }


}
